package com.example.easyutils.musicpage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.easyutils.BaseActivity;
import com.example.easyutils.MainActivity;
import com.example.easyutils.R;
import com.example.easyutils.garbagepage.Main;
import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.util.MusicIdUtil;
import com.example.easyutils.util.MyTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class MusicType extends BaseActivity {
    private GridView musicGridView;
    private List musicList;
//    private String[] musicType = new String[]{"3779629","3778678","2884035","19723756","10520166",
//    "180106","60198","21845217","11641012","120001","60131","3733003","60255","46772709","112504",
//    "64016","10169002","4395559","1899724","27135204","112463","3812895"};
    private static final int GET_MUSIC_LIST_CODE = 001,CODE_ERROR = 404;
    private List<MusicUtil> list;
    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case GET_MUSIC_LIST_CODE:
                    Log.d("列表信息",musicList+"");
                    list = new ArrayList<>();
                    for(int i = 0; i < musicList.size(); i ++){
                        String name = ((Map)musicList.get(i)).get("name").toString();
                        String id = ((Map)musicList.get(i)).get("id").toString();
                        String coverImgUrl = ((Map)musicList.get(i)).get("coverImgUrl").toString();
                        MusicUtil musicUtil = new MusicUtil(name,coverImgUrl);
                        list.add(musicUtil);
                    }
                    MusicAdapter musicAdapter = new MusicAdapter(MusicType.this,R.layout.music_type_item,list);
                    musicGridView.setAdapter(musicAdapter);
                break;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_type);
        musicGridView = (GridView)findViewById(R.id.music_gridView);
        PlayMusicPage playMusicPage = new PlayMusicPage();
        String playMusicId = playMusicPage.globalMusicId;

        musicGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MusicType.this,MusicMain.class);
                intent.putExtra("musicId",((Map)musicList.get(position)).get("id").toString());
                intent.putExtra("musicListName",((Map)musicList.get(position)).get("name").toString());
                intent.putExtra("activityName","MusicType");
                startActivity(intent);
            }
        });
        Log.d("这是选择类型页面","test");
        getMisicList();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    public void playMusic(View v){
        Intent intent = new Intent();
        intent.setClass(MusicType.this, MusicMain.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//设置不要刷新将要跳到的界面
        intent.putExtra("activityName","PlayMusicPage");
        startActivity(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d("返回键",keyCode+"");
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Intent intent = new Intent();
            intent.setClass(MusicType.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);//设置不要刷新将要跳到的界面
//            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//它可以关掉所要到的界面中间的activity
            startActivity(intent);
//            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }

    private void getMisicList(){//获取榜单
        HttpGetRequest.sendOkHttpGetRequst("http://116.62.147.89:8083/getMusicList",
                new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("获取到失败列表",e+"");
                        handler.sendEmptyMessage(CODE_ERROR);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String dataMap = response.body().string();

                        Gson gson = new GsonBuilder()
                                .registerTypeAdapter(new TypeToken<Map<String, List>>(){

                                }.getType(),new MyTypeAdapter()).create();
                        Map<String,List> dataDetail = gson.fromJson(dataMap,new TypeToken<Map<String,List>>(){

                        }.getType());
                        musicList = new ArrayList();
                        musicList = dataDetail.get("list");
                        Log.d("获取到成功列表",dataDetail+"");
                        handler.sendEmptyMessage(GET_MUSIC_LIST_CODE);
                    }
                });
    }
}
